package com.stevenmoon.csdnplus.logic;

import com.google.gson.reflect.TypeToken;
import com.stevenmoon.csdnplus.bean.headline.ColumnHeadlineReqBean;
import com.stevenmoon.csdnplus.bean.headline.CommentInfoBean;
import com.stevenmoon.csdnplus.bean.headline.CommentListReqBean;
import com.stevenmoon.csdnplus.bean.headline.CommunityDetailBean;
import com.stevenmoon.csdnplus.bean.headline.DetailReqBean;
import com.stevenmoon.csdnplus.bean.headline.DetailUpDownInfo;
import com.stevenmoon.csdnplus.bean.headline.DetailUpDownReqBean;
import com.stevenmoon.csdnplus.bean.FavoriteOperationInfo;
import com.stevenmoon.csdnplus.bean.FavoriteOperationReqBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineColumnBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineCommentInfo;
import com.stevenmoon.csdnplus.bean.headline.HeadlineCommentReqBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineCommentUpDownInfo;
import com.stevenmoon.csdnplus.bean.headline.HeadlineDetailBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineInfoBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineListBean;
import com.stevenmoon.csdnplus.bean.headline.LatestNewsReqBean;
import com.stevenmoon.csdnplus.bean.ResponseResultBean;
import com.stevenmoon.csdnplus.bean.UserRelationBean;
import com.stevenmoon.csdnplus.net.RequestParams;
import com.stevenmoon.csdnplus.net.RxNet;
import com.stevenmoon.csdnplus.util.GsonUtil;

import java.util.List;

import rx.Observable;

import static com.stevenmoon.csdnplus.app.Constants.CSDNURL;

/**
 * Created by WangJun on 2016/4/10.
 */
public class HeadlineManager {

    public static Observable<List<HeadlineColumnBean>> getHeadlineColumns(){
        RequestParams rp = new RequestParams(CSDNURL.HEADLINE_COLUMNS, null);
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<List<HeadlineColumnBean>>>(){});
    }

    public static Observable<List<HeadlineInfoBean>> getLatestNews(LatestNewsReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.LATEST_NEWS, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<List<HeadlineInfoBean>>>(){});
    }

    public static Observable<List<HeadlineListBean>> getColumnHeadlineList(ColumnHeadlineReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.COLUMNS_HEADLINE_LIST, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<List<HeadlineListBean>>>(){});
    }

    public static Observable<HeadlineDetailBean> getHeadlineDetail(DetailReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.HEADLINE_DETAIL, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<HeadlineDetailBean>>(){});
    }

    public static Observable<UserRelationBean> getRelationWith(String username){
        RequestParams rp = new RequestParams(CSDNURL.USER_RELATION);
        rp.put("username",username);
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<UserRelationBean>>(){});
    }

    public static Observable<CommunityDetailBean> getCommunityDetail(String childcommunityid){
        RequestParams rp = new RequestParams(CSDNURL.COMMUNITY_DETAIL);
        rp.put("childcommunityid",childcommunityid);
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<CommunityDetailBean>>(){});
    }

    public static Observable<CommentInfoBean> getCommentList(CommentListReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.COMMENTS_LIST, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<CommentInfoBean>>(){});
    }

    public static Observable<DetailUpDownInfo> doDetailUpDown(DetailUpDownReqBean reqBean){
        String url ;
        if(reqBean.getType() == DetailUpDownReqBean.TYPE_NEWS){
            url = CSDNURL.NEWS_UP_DOWN;
        }else if(reqBean.getType() == DetailUpDownReqBean.TYPE_GEEK){
            url = CSDNURL.GEEK_UP_DOWN;
        }else{
            url = CSDNURL.CONTENT_CENTER_UP_DOWN;
        }
        RequestParams rp = new RequestParams(url, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<DetailUpDownInfo>>(){});
    }

    public static Observable<FavoriteOperationInfo> addFavorite(FavoriteOperationReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.ADD_FAVORITE, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<FavoriteOperationInfo>>(){});
    }

    public static Observable<FavoriteOperationInfo> deleteFavorite(FavoriteOperationReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.DELETE_FAVORITE, GsonUtil.convertBean2Map(reqBean));
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<FavoriteOperationInfo>>(){});
    }

    public static Observable<HeadlineCommentUpDownInfo> doCommentUp(String username, String commentId){
        RequestParams rp = new RequestParams(CSDNURL.HEADLINE_COMMENT_UP);
        rp.put("username",username);
        rp.put("id",commentId);
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<HeadlineCommentUpDownInfo>>(){});
    }

    public static Observable<HeadlineCommentUpDownInfo> doCommentDown(String username, String commentId){
        RequestParams rp = new RequestParams(CSDNURL.HEADLINE_COMMENT_DOWN);
        rp.put("username",username);
        rp.put("id",commentId);
        return RxNet.getAndFetchEntity(rp,new TypeToken<ResponseResultBean<HeadlineCommentUpDownInfo>>(){});
    }

    public static Observable<HeadlineCommentInfo> addComment(HeadlineCommentReqBean reqBean){
        RequestParams rp = new RequestParams(CSDNURL.HEADLINE_COMMENT_ADD, GsonUtil.convertBean2Map(reqBean));
        return RxNet.postAndFetchEntity(rp,new TypeToken<ResponseResultBean<HeadlineCommentInfo>>(){});
    }


}
